package com.rainy.cloud.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author user
 */
@Component
public class AuthFilter implements GlobalFilter, Ordered {

    private static final String HEADER_TOKEN_NAME = "123";

//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        Mono<Void> mono = null;
//        String token = exchange.getRequest().getHeaders().getFirst("token");
//        if (HEADER_TOKEN_NAME.equals(token)) {
//            mono = chain.filter(exchange);
//        }
//        if (!HEADER_TOKEN_NAME.equals(token)) {
//            ServerHttpResponse response = exchange.getResponse();
//            Response data = new Response();
//            data.setCode(ExceptionCode.VALIDATE_EXCEPTION);
//            data.setErrMsg("非法请求");
//            byte[] newRs = new Gson().toJson(data).getBytes(StandardCharsets.UTF_8);
//            DataBuffer buffer = response.bufferFactory().wrap(newRs);
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//            mono = response.writeWith(Mono.just(buffer));
//        }
//        return mono;
//    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        Mono<Void> mono = chain.filter(exchange);
        return mono;
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 1;
    }
}
